package com.sss.hloster0303;
//
import java.util.Scanner;

public class BinarySearchIndex {

	public static void main(String[] args) {
		test01();
	}

	private static void test01() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入有序数组：");
		System.out.println("输入的数组有多少元素：");
		int sum = sc.nextInt();
		int a[] = new int[sum];
		for (int i = 0; i < sum; i++) {
			System.out.println("输入的第" + (i + 1) + "个元素：");
			a[i] = sc.nextInt();

		}
		System.out.println("请输入需要查找的元素：");
		int n = sc.nextInt();
		int x = binary(a, n);
		if (x == -1) {
			System.out.println("元素不存在啊");
			
		}else {
			System.out.println("元素存在啊");
		}
	}

	public static int binary(int[] arr, int key) {
		int low = 0;
		int high = arr.length - 1;
		int mid = 0;
		while (low <= high) {
			mid = (low + high) / 2;
			if (key == arr[mid]) {
				return mid;

			} else if (key < arr[mid]) {
				high = mid - 1;
			} else {
				low = mid + 1;
			}

		}
		return -1;
	}
}
